package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.entity.UserRole;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> selectRolePage(Integer pageNum, Integer pageSize, RoleDto roleDto) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> list = roleMapper.getPage(roleDto.getRoleName());
        return PageResponse.of(list, RoleVo.class);
    }

    @Override
    public void addRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        if (role.getDataState()==null){
            role.setDataState(SuperConstant.DATA_STATE_0);
        }
        roleMapper.addRole(role);
    }

    @Override
    public List<String> findCheckedResources(Long roleId) {
        return roleMapper.findCheckedResources(roleId);
    }

    @Override
    @Transactional
    public void updateRole(RoleDto roleDto) {
        Long roleId = roleDto.getId();
        Role role = roleMapper.selectRoleById(roleId);
        if (role == null) {
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        if (roleDto.getRoleName()!=null){
            role.setRoleName(roleDto.getRoleName());
            roleMapper.updateRole(role);
        }
        if (roleDto.getDataState()!=null){
            //如果用户已经关联角色则不能禁用
            List<UserRole> userRoleList = userRoleMapper.queryByRoleId(roleId);
            if (StrUtil.equals(roleDto.getDataState(), SuperConstant.DATA_STATE_1) && CollectionUtil.isNotEmpty(userRoleList)) {
                throw new BaseException(BasicEnum.MENU_DISTRIBUTED);
            }
            role.setDataState(roleDto.getDataState());
            roleMapper.updateRole(role);
        }
        String[] checkedResourceNos = roleDto.getCheckedResourceNos();
        if (ArrayUtil.isNotEmpty(checkedResourceNos)) {
            // 删除角色和资源关联数据
            roleResourceMapper.deleteByRoleId(role.getId());
            //新增角色和资源关联数据
            List<RoleResource> roleResourceList = Arrays.stream(checkedResourceNos).map(resourceNo -> {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(role.getId());
                roleResource.setResourceNo(resourceNo);
                roleResource.setDataState(SuperConstant.DATA_STATE_0);
                return roleResource;
            }).collect(Collectors.toList());
            for (RoleResource roleResource : roleResourceList) {
                roleResourceMapper.insertBatch(roleResource);
            }
        }
    }

    @Override
    @Transactional
    public void deleteById(Long roleId) {
        Role role = roleMapper.selectRoleById(roleId);
        if (role == null) {
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        if (StrUtil.equals(role.getDataState(), SuperConstant.DATA_STATE_0)) {
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        roleMapper.deleteById(roleId);
        roleResourceMapper.deleteByRoleId(roleId);
    }

    @Override
    public List<RoleVo> initRoles() {
        return roleMapper.selectAllRole();
    }
}
